// pages/edu/exam/exam/index.js
import{ExamJobs} from '../../model/exam_jobs.js'
const ExamJobsModel=new ExamJobs()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    list:{},
    formData:{},
    formRules:[],
    flag:false,
    bank_id:0,
    subject_id:0,
    timeEnd:false,
    timeout:0,
    is_pass:false,
    itemKey:'A'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.data.id=options.id
    }
    wx.setNavigationBarTitle({
      title: '在线考试-艾族大学',
    })
    this._getSubjectToQuestion()
  },

  //---------------------------

  /**
   * 获取问题
   */
  _getSubjectToQuestion(){
    let id=this.data.id
    ExamJobsModel.getSubjectToQuestion(id,(res)=>{
      this.setData({
        list:res.get_exam_question,
        bank_id:res.id,
        subject_id:res.subject_id,
        timeout:res.get_exam_subject.timeout*60,
        timeEnd:true
      })
      this._getFormRules()
    },(res)=>{
        if(res.msg_code==30002){
          this.setData({is_pass:true,'describe':res.msg})
        }
    })
  },

  /**
   * 表单验证项
   */
  _getFormRules(){
    let rules=[]
    let list=this.data.list
    for(var i=0;i<list.length;i++){
      if(list[i].get_exam_question_item.length>0){
        let question_item=list[i].get_exam_question_item
        for(var item=0;item<question_item.length;item++){
          rules=rules.concat(question_item[item].id)
        }
      }
    }
    this.setData({formRules:rules})
  },

  /**
   * 设置表单数据
   * @param {*} id 
   * @param {*} value 
   */
  _setFormData(id,value){
    let data=this.data.formData
    data[id]=value
    this.data.formData=data
  },

  /**
   * 删除表单数据
   * @param {*} id 
   */
  _unsetFormData(id){
    let data=this.data.formData
    delete data[id]
    this.data.formData=data
  },

  /**
   * 考试结束
   */
  onTimeEnd(){
    wx.lin.showToast({
      title: "考试时间已到，正在提交试卷~"
    })
    this.setData({timeEnd:false})
    setTimeout(()=>{
      this.submitData('',(res)=>{
        if(res.msg_code==30001){
          wx.lin.showToast({
            title: "未答题，请重新考核~",
            icon:'error'
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            });
          },1500)
        }
      })
    },1500) 
  },

  /**
   * 判断题
   * @param {p} event 
   */
  onBinary(event){
    let id=event.currentTarget.dataset.id
    let value=event.detail.value
    this._setFormData(id,value)
  },

  /**
   * 单选题
   * @param {*} event 
   */
  onSingle(event){
    let id=event.currentTarget.dataset.id
    let value=[event.detail.value]
    this._setFormData(id,value)
  },

  /**
   * 多选题
   * @param {*} event 
   */
  onMultiple(event){
    let id=event.currentTarget.dataset.id
    let all=event.detail.all
    let value=[]
    if(all.length>0){
      all.forEach(function(item,index){
        value.push(item.value)
      })
      this._setFormData(id,value)  
    }else{
      this._unsetFormData(id)
    }
  },

  /**
   * 填空题
   * @param {*} event 
   */
  onFill(event){
    let id=event.currentTarget.dataset.id
    let value=event.detail.detail.value
    this._setFormData(id,value)
  },

  /**
   * 交卷
   * @param {*} event 
   */
  onSubmit(){
    if(!this.data.timeEnd){
      wx.lin.showToast({
        title:'答题时间已到，不能提交~'
      })
      return ;
    }
    if(this.checkValidate()){
      this.submitData()
      this.setData({timeEnd:false})
    }
  },

  /**
   * 验证表单 
   */
  checkValidate(){
    let formData=this.data.formData
    let formRules=this.data.formRules
    let flag=true;
    let _this=this
  
    if(JSON.stringify(formData)=="{}"){
      wx.lin.showMessage({
        type:'warning',
        content:'请先完成答题，再交卷！'
      })
      return false;
    }

    formRules.forEach(function(item,index){
      if(!formData[item]){
        wx.lin.showMessage({
          type:'warning',
          content:'请先完成答题，再交卷！'
        })
        flag=false
        _this._setFormData(item,'')
      }
    })
    return flag;
  },

  /**
   *  提交答题内容
   */
  submitData(success,fail){
    let formData=this.data.formData
    let data={
      bank_id:this.data.bank_id,
      form_data:formData,
      subject_id:this.data.subject_id
    }
    wx.showLoading({
      title: '提交中~',
    });
    ExamJobsModel.setExamQuestion(data,(res)=>{
      wx.hideLoading();
      if(res.code){
        wx.lin.showToast({
          title: res.msg,
          icon: 'success'
        })
        success&&success(res)
        setTimeout(function(){
          wx.redirectTo({
            url: '../exam_result/index?id='+res.data.id
          });
        },1500)
      }
    },(res)=>{
      wx.hideLoading();
      if(fail){
        fail(res)
      }else{
        wx.lin.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  }
})